package com.mxsat.system.tree;

import java.util.ArrayList;
import java.util.List;

import com.mxsat.common.layui.tree.LayTree;
import com.mxsat.common.layui.tree.TreeStrategy;
import com.mxsat.system.entity.SysMenu;

public class MenuTreeStrategy implements TreeStrategy{
	
	private List<SysMenu> menus;
	
	public MenuTreeStrategy(List<SysMenu> menus) {
		this.menus = menus;
	}

	@Override
	public List<LayTree> transforLayTree() throws Exception {
		List<LayTree> list = new ArrayList<>();
		for (SysMenu menu : menus) {
			if((new Long(0)).equals(menu.getId_parent())){
				MenuTree m = new MenuTree(menu);
				findChildren(m);
				list.add(m);
			}
		}
		return list;
	}

	private void findChildren(final MenuTree menuTree) throws Exception {
		List<LayTree> list = new ArrayList<>();
		for (SysMenu m : menus) {
			if (menuTree.getId().equals(m.getId_parent()+"")) {
				final MenuTree temp = new MenuTree(m);
				this.findChildren(temp);
				list.add(temp);
			}
		}
		menuTree.setChildren(list);
	}
	
}
